package com.nono.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nono.Dao.AdminDao;
import com.nono.Dao.CommentDao;
import com.nono.bean.Comment;
import com.nono.bean.Type;

@Controller
@RequestMapping(value="admin")
public class AdminController {
	
	@Autowired
	AdminDao adminDao;
	
	@Autowired
	CommentDao commentDao;
	
	@RequestMapping(value="getTypes", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getTypes() {
		JSONArray jsonArray = JSONArray.fromObject( adminDao.getTypes() );
		return jsonArray;
	}


	@RequestMapping(value="addType", method=RequestMethod.POST)
	@ResponseBody
	public boolean addType(HttpServletRequest request, HttpServletResponse response) {
		String type = request.getParameter("type");
		return adminDao.addType(type);
	}

	@RequestMapping(value="delType", method=RequestMethod.POST)
	@ResponseBody
	public boolean delType(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		return adminDao.delType(id);
	}
	

	@RequestMapping(value="addPro", method=RequestMethod.POST)
	@ResponseBody
	public boolean addPro(HttpServletRequest request, HttpServletResponse response) {
		//String name, String depict, int price, int amount, String manufacturer, String img, String type
		String name = request.getParameter("name");
		String depict = request.getParameter("depict");
		int price = Integer.parseInt( request.getParameter("price") );
		int amount = Integer.parseInt( request.getParameter("amount") );
		String manufacturerString = request.getParameter("manufacturer");
		String img = request.getParameter("img");
		String type = request.getParameter("type");
		
		return adminDao.addPro( name,  depict,  price,  amount,  manufacturerString,  img,  type);
	}
	
	@RequestMapping(value="delPro", method=RequestMethod.POST)
	@ResponseBody
	public boolean delPro(HttpServletRequest request, HttpServletResponse response) {
		//String name, String depict, int price, int amount, String manufacturer, String img, String type
		int id = Integer.parseInt(request.getParameter("id") );
		return adminDao.delPro( id );
	}
	
	@RequestMapping(value="getAllCom", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getAllCom(HttpServletRequest request, HttpServletResponse response) {
		return JSONArray.fromObject( adminDao.getAllCom( ) );
	}
	
	@RequestMapping(value="getComByType", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getComByType(HttpServletRequest request, HttpServletResponse response) {
		String type = request.getParameter("type");
		return JSONArray.fromObject( adminDao.getComByType( type ) );
	}


	@RequestMapping(value="getComments", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getComments(HttpServletRequest request, HttpServletResponse response) {
		return JSONArray.fromObject( commentDao.getComments( ) );
	}
	@RequestMapping(value="getCommentById", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getCommentById(HttpServletRequest request, HttpServletResponse response) {
		String commodityId = request.getParameter("commodityId");
		return JSONArray.fromObject( commentDao.getCommentById( Integer.parseInt(commodityId) ) );
	}
	
}
